package com.mystore.dao.impl;

import com.mystore.dao.BaseDao;
import com.mystore.dao.OrderDao;
import com.mystore.entity.Order;
import com.mystore.vo.OrderVO;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    private BaseDao baseDao;

    public OrderDaoImpl() {
        this.baseDao =new BaseDaoImpl();
    }

    /**
     * 查找订单信息
     * @param uid
     * @param connection
     * @return
     */
    @Override
    public List<OrderVO> findAllOrderByUid(int uid, Connection connection) {
        String sql = "select o.id,o.price,o.status,o.startAddress,g.name,g.msg,g.price,g.img,o.num,a.address, a.tel  from order o,address a,good g where o.uid=? and o.uid=a.uid and a.id= o.address and o.gid = g.id";
        List<Object> list = new ArrayList<>();
        list.add(uid);
        List<OrderVO> voList = null;
        try {
            voList = baseDao.findMoreRefResult(sql,list,OrderVO.class,connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return voList;
    }

    /**
     * 保存订单信息
     * @param order
     * @param connection
     * @return
     * @throws SQLException
     */
    @Override
    public Boolean save(Order order, Connection connection) throws SQLException {
        String sql = "insert into order(uid,gid,price,num,address,status,startAddress) values(?,?,?,?,?,?,?)";
        List<Object> list = new ArrayList<>();
        list.add(order.getUid());
        list.add(order.getGid());
        list.add(order.getPrice());
        list.add(order.getNum());
        list.add(order.getAddress());
        list.add(order.getStatus());
        list.add(order.getStartAddress());

        return baseDao.updateByPreparedStatement(sql,list,connection);
    }

    /**
     *
     * @param oid
     * @param uid
     * @param connection
     * @return
     */
    @Override
    public OrderVO findOneOrderByOidAndUid(int oid, int uid,Connection connection) {
        String sql = "select o.id,o.price,o.status,o.startAddress,g.name,g.msg,g.price,g.img,o.num,a.address, a.tel  from order o,address a,good g where o.id=? and o.uid=? and o.uid=a.uid and a.id= o.address and o.gid = g.id";
        List<Object> list = new ArrayList<>();
        list.add(oid);
        list.add(uid);
        OrderVO orderVO = null;
        try {
            orderVO = (OrderVO) baseDao.findSingleResult(sql,list,OrderVO.class,connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderVO;
    }

    /**
     * 更新订单信息
     * @param order
     * @param connection
     * @return
     * @throws SQLException
     */
    @Override
    public Boolean updateOrder(Order order, Connection connection) throws SQLException {
        String sql = "update order set status=? where id=? and uid=?";
        List<Object> list = new ArrayList<>();
        list.add(order.getStatus());
        list.add(order.getId());
        list.add(order.getUid());
        return baseDao.updateByPreparedStatement(sql,list,connection);
    }
}
